var util = require('../../utils/util');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
import { $wuxLoading } from '../../dist/index'
var app = getApp();
var that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    machineNo:'',
    washtype :[],
    // washtype: [{ "washtype": "快速洗" }, {"washtype":"慢速次"}],
    action:'',
    total:'',
    show:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	util.loading = $wuxLoading();
 
    var that = this;
    that.setData({
      machineNo: options.machine_code
    })
    var  machineno = options.machine_code ;

	util.showLoading();

     var url = config.queryWashTypeAndPrice;
     var params = {
      machineno: machineno
     }
     wxRequest.postRequest(url, params) 
     .then(res=>{
         if(res.length==0){
           util.alert("洗衣机异常,暂停使用",function(){
           })
         }else{
          that.setData({
            washtype: res
          })

         }
     })
     .catch(res => {
         util.alert_catch(res.message);
     })
     .finally(function (res) {
         util.hideLoading();
     });


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  goWash:function(){ 
    var params = {};
    params.machineno = this.data.machineNo;
    params.action = this.data.action;
    
	if(!params.action){
		util.alert("请选择洗衣方式");
		return;
	}
	
	util.showLoading();
	
    wxRequest.postRequest(config.scanpay, params)
    .then(res => {
        wx.requestPayment({
          'appId': res.appId,
          'timeStamp': res.timeStamp + "",
          'nonceStr': res.nonceStr,
          'package': 'prepay_id='+res.prepay_id,
          'signType': 'MD5',
          'paySign': res.paySign,
          'success': function (res) {
            wx.showToast({
              title: "充值成功",
              icon: 'success',
              duration: 3000,
              mask: true
            });
            setTimeout(function () {
              wx.switchTab({
                url: '../../pages/index/index',
              })
            }, 3000) 
          },
          'fail': function (res) {
            console.log(res);
          }
        })
    })
    .catch(res => {
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });

  },
  chose:function(optioins){
    var typewash = optioins.target.id;
    var that = this;
    var price =5;
    for (var i = 0; i < that.data.washtype.length; i++) {
      if (that.data.washtype[i].washtype == typewash){
        price = that.data.washtype[i].price;
      }
    };
    price = price * 100;  
    switch (typewash){
      case '标准洗':
        that.setData({
          action: "biaozhun",
          total: price
        });
        break;
      case '单脱水':
        this.setData({
          action: "dantuo",
          total: price
        });
        break;
      case '快速洗':
        this.setData({
          action: "kuaixi",
          total: price
        });
        break; 
      case '大物洗':
        this.setData({
          action: "dawu",
          total: price
        });
        break; 
    };
    this.setData({
      show:true
    });

  }


})